#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll mod = 998244353;

struct node {
	ll x, y;
} p1, p2, p3, mn;

ll T, n, s1, s2, s3, M, ans, t;
ll a[10001000];
ll s[10001000];
double p, mx, l, r;
double g1, g2;

ll qpow(ll x, ll y) {
	if (!y) {
		return 1;
	}
	ll ss = qpow(x, y >> 1);
	if (y & 1) {
		return ss * ss % mod * x % mod;
	} else {
		return ss * ss % mod;
	}
}

inline bool check(double x) {
	for (ll i = 1; i <= t; i++) {
		if (s[i] == 1) {
			g2 = 1.0;
		} else {
			g2 = 1.0 - x;
		}
		if (i == 1) {
			g1 = 2 * x - g2;
		} else {
			g2 -= g1;
			if (g2 < 0) {
				return 0;
			}
			g1 = 2 * x - g2;
		}
		if (g1 < 0) {
			g1 = 0;
		}
	}
	return 1;
}

int main() {
	freopen("guess.in", "r", stdin);
	freopen("guess.out", "w", stdout);
	scanf("%lld", &T);
	while (T--) {
		ans = 0;
		scanf("%lld%lld%lld%lld%lld%lld", &n, &a[0], &s1, &s2, &s3, &M);
		s1 %= M;
		s2 %= M;
		s3 %= M;
		for (ll i = 1; i <= n; i++) {
			a[i] = (s1 * a[i - 1] % M * a[i - 1] % M + s2 * a[i - 1] % M + s3) % M + 1;
			s[a[i]]++;
		}
		t = 0;
		for (ll i = 1; i <= n; i++) {
			if (s[i]) {
				s[++t] = s[i];
			}
		}
		s[t + 1] = 0;
		l = 0;
		r = 1;
		p = 0;
		while (r - l > 1e-8) {
			double mid = (l + r) / 2.0;
			if (check(mid)) {
				p = mid;
				l = mid + 1e-8;
			} else {
				r = mid - 1e-8;
			}
		}
		if (s[1] == 1) {
			p3.x = 1;
			p3.y = 0;
		} else {
			p3.x = 1;
			p3.y = -1;
		}
		mx = 1;
		mn.x = 1;
		mn.y = 1;
		for (ll i = 2; i <= t; i++) {
			p1.x = -p3.x;
			p1.y = 2 - p3.y;
			if (1.0 * abs(p1.x) / abs(p1.y) > p) {
				p1.x = p1.y = 0;
			}
			mx = min(mx, 1.0 * abs(p1.x) / abs(p1.y));
			if (s[i] == 1) {
				p2.x = 0;
				p2.y = 0;
			} else {
				p2.x = 0;
				p2.y = 1;
			}
			p3.x = 1 - p1.x - p2.x;
			p3.y = -p1.y - p2.y;
			if (abs(p3.x) * mn.y < abs(p3.y) * mn.x) {
				mn.x = abs(p3.x);
				mn.y = abs(p3.y);
			}
		}
		ans = mn.x * qpow(mn.y, mod - 2) % mod;
		p = 1.0 * mn.x / mn.y;
		printf("%.5lf %lld\n", p, ans);
		for (ll i = 1; i <= n; i++) {
			s[i] = 0;
		}
	}
	return 0;
}
